<%--
  Created by IntelliJ IDEA.
  User: 东科教育
  Date: 2021/5/11
  Time: 9:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/common.jsp"%>
<html>
<head>
    <title>Title</title>
    <SCRIPT type="text/javascript">

        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            check: {
                enable: true,
                autoCheckTrigger: true
            },
            async: {
                enable: true,
                url: "<%=path%>/menu/getInfoZtree",
            },
            view : {
                addHoverDom: function(treeId, treeNode){
                    var aObj = $("#" + treeNode.tId + "_a");
                    if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                    var s = '<span id="btnGroup'+treeNode.tId+'">';
                    if ( treeNode.level == 0 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                    } else if ( treeNode.level == 1 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                        if (treeNode.children == undefined) {
                            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
                        }
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                    } else if ( treeNode.level == 2 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
                    }
                    s += '</span>';
                    aObj.after(s);
                },
                removeHoverDom: function(treeId, treeNode){
                    $("#btnGroup"+treeNode.tId).remove();
                }
            }
        };

        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting);

        });
        function addNode(pid){
            BootstrapDialog.show({
                title: '增加',
                message: $('<div></div>').load('<%=path%>/menu/toAdd'),
                closeable: true,
                buttons: [{
                    label: '确定',
                    action: function (dialog) {
                        $.ajax({
                            url : "<%=path%>/menu/add",
                            type : "post",
                            dataType : "json",
                            data : $("#menu_add").serialize() + "&pid=" + pid,
                            success : function(data){
                                if(data){
                                    toastr.success("增加成功")
                                    dialog.close();
                                    $.fn.zTree.init($("#treeDemo"), setting);
                                }
                            },
                            error : function(data){
                                console.log(data)
                                toastr.error("增加操作执行失败")
                            }
                        })
                    }
                }]
            });
        }
        function deleteNode(id){
           if(confirm("真的要狠心吗")){
               $.ajax({
                   url:"<%=path%>/menu/deleteById",
                   data:{id:id},
                   type:"get",
                   dataType:"json",
                   async:true,
                   success:function (result){
                       toastr.success("删除成功");
                       $.fn.zTree.init($("#treeDemo"), setting);
                   },
                   error:function (result){
                       console.log(id);
                       toastr.error("执行操作失败");
                   }
               })
           }
        }
        function editNode(id){
            BootstrapDialog.show({
                title: '修改',
                message: $('<div></div>').load('<%=path%>/menu/toUpdate/'+id),
                closeable: true,
                buttons: [{
                    label: '确定',
                    action: function (dialog) {
                        $.ajax({
                            url : "<%=path%>/menu/update",
                            type : "post",
                            dataType : "json",
                            data : $("#menu_update").serialize(),
                            success : function(data){
                                    toastr.success("更新成功")
                                    dialog.close();
                                    $.fn.zTree.init($("#treeDemo"), setting);
                            },
                            error : function(data){
                                console.log(data)
                                toastr.error("更新操作执行失败")
                            }
                        })
                    }
                }]
            });
        }
    </SCRIPT>
</head>
<body>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
</div>
</body>
</html>
